//
//  MyAPI.m
//  ConsumeTreasure
//
//  Created by youyou on 11/1/16.
//  Copyright © 2016 youyou. All rights reserved.
//
#import "AppDelegate.h"
#import "MyAPI.h"
#import <AFNetworking.h>
//#define baseUrl @"http://192.168.1.108/"


#define baseUrl @"192.168.1.105/api/v1"

@interface MyAPI()
@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end
@implementation MyAPI
- (id)init{
    self = [super init];
    if (self) {
        self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];
        //申明返回的结果是json类型
            self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
        //    //如果报接受类型不一致请替换一致text/html或别的
            self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];

    }
    return self;
}
+ (MyAPI *)sharedAPI{
    static MyAPI *sharedAPIInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAPIInstance = [[self alloc] init];
    });
    //1.创建网络状态监测管理者
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    //开启监听，记得开启，不然不走block
    [manger startMonitoring];
    //2.监听改变
    [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        /*
         AFNetworkReachabilityStatusUnknown = -1,
         AFNetworkReachabilityStatusNotReachable = 0,
         AFNetworkReachabilityStatusReachableViaWWAN = 1,
         AFNetworkReachabilityStatusReachableViaWiFi = 2,
         */
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"netIsNotReachabel" object:nil];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G|4G");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WiFi");
                break;
            default:
                break;
        }
    }];
    [manger stopMonitoring];
    return sharedAPIInstance;
  
}
- (void)cancelAllOperation{
    [self.manager.operationQueue cancelAllOperations];
}
#pragma mark - 红包接口
- (void)loadLuckyMoneyWithParameters:(NSDictionary *)parameters withResult:(ModelBlock)Result{
    [self.manager POST:@"/ActiveLine" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - 登录

- (void)loginWithParameters:(NSDictionary *)parameters withResult:(ModelBlock)Result{
    [self.manager POST:MYY_login_URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"---login --%@",responseObject);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - 消息列表

- (void)loadMessageWithParameters:(NSDictionary *)parameters withResult:(ModelBlock)Result{
    
    [self.manager POST:MYY_Message_URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
/**
 *  打印HTTP请求的URL
 *
 *  @param urlString URL字符串
 *  @param params    参数字典
 */
- (void)printRequestUrl:(NSString *)urlString params:(NSDictionary *)params {
    NSMutableString *totalUrl = [NSMutableString stringWithFormat:@"%@?", urlString];
    [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (obj) {
            [totalUrl appendString:[NSString stringWithFormat:@"&%@=%@", key, obj]];
        }
    }];
    NSLog(@"%@请求 = %@", [params.allKeys containsObject:@"method"] && [[params[@"method"] uppercaseString] isEqualToString:@"POST"] ? @"POST" : @"GET", totalUrl);
}

@end
